package com.up.vms.interfaces.rest.security;

import com.alibaba.fastjson.JSON;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
import com.up.vms.infrastructure.mvc.RespCode;
import com.up.vms.infrastructure.mvc.ReturnCode;
import com.up.vms.interfaces.rest.response.CommonResponse;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * <p>
 * 自定义退出成功记录
 * </p>
 *
 */
public class CustomLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        CommonResponse systemResponseMsg = new CommonResponse();
        RespCode responseCode = new RespCode(ReturnCode.SUCCESS);
        systemResponseMsg.setRespCode(responseCode);
        systemResponseMsg.setData(null);

        PrintWriter writer = response.getWriter();
        writer.write(JSON.toJSONString(systemResponseMsg));

        super.onLogoutSuccess(request, response, authentication);
    }
}
